﻿var dialog		= window.parent ;
var oEditor		= dialog.InnerDialogLoaded() ;
var oImageOriginal = dialog.Selection.GetSelectedElement() ;
if ( oImageOriginal && oImageOriginal.tagName != 'IMG' && !( oImageOriginal.tagName == 'INPUT' && oImageOriginal.type == 'image' ) ) 
    oImageOriginal = null ;
var oLink = dialog.Selection.GetSelection().MoveToAncestorNode( 'A' ) ;
/************************************************/
function LoadSelection()
{
    if(!oImageOriginal)
        return;
    GetE('imgupload').disabled = true;
	var sUrl = oImageOriginal.getAttribute( '_fcksavedurl' ) ;
	if ( sUrl == null || sUrl=='')
		sUrl = GetAttribute( oImageOriginal, 'src', '' ) ;
	GetE('imgSource').innerHTML = '<input id="imgupload" type="text" value="'+sUrl+'" size="35" />';
	GetE('description').value = GetAttribute( oImageOriginal, 'alt', '' ) ;
	GetE('vspace').value	= GetAttribute( oImageOriginal, 'vspace', '' ) ;
	GetE('hspace').value	= GetAttribute( oImageOriginal, 'hspace', '' ) ;
	GetE('oborder').value	= GetAttribute( oImageOriginal, 'border', '' ) ;
	var oAlign = GetE('oalign');
	var align = GetAttribute( oImageOriginal, 'align', '' ).toLowerCase() ;
    for (i = 0;i<oAlign.length;i++){
        if (oAlign[i].value.toLowerCase() == align)
        {
            oAlign.selectedIndex = i;
            break;
        }
    }
    var oClass = GetE('ostyle');
	var _class = GetAttribute( oImageOriginal, 'class', '' ).toLowerCase() ;
    for (i = 0;i<oClass.length;i++){
        if (oClass[i].value.toLowerCase() == _class)
        {
            oClass.selectedIndex = i;
            break;
        }
    }
	var iWidth, iHeight ;

	var regexSize = /^\s*(\d+)px\s*$/i ;
	if ( oImageOriginal.style.width )
	{
	   
		var aMatchW  = oImageOriginal.style.width.match( regexSize ) ;
		if ( aMatchW )
		{
			iWidth = aMatchW[1] ;
			oImageOriginal.style.width = '' ;
			SetAttribute( oImageOriginal, 'width' , iWidth ) ;
		}
	}

	if ( oImageOriginal.style.height )
	{
		var aMatchH  = oImageOriginal.style.height.match( regexSize ) ;
		if ( aMatchH )
		{
			iHeight = aMatchH[1] ;
			oImageOriginal.style.height = '' ;
			SetAttribute( oImageOriginal, 'height', iHeight ) ;
		}
	}

    iWidth = iWidth ? iWidth : GetAttribute( oImageOriginal, "width", '' ) ;
    iWidth = iWidth ? iWidth : oImageOriginal.width;
    iHeight = iHeight ? iHeight : GetAttribute( oImageOriginal, "height", '' ) ;
	iHeight = iHeight ? iHeight : oImageOriginal.height;
	GetE('image_width').value	= iWidth;
	GetE('image_height').value	= iHeight;
	if ( oLink )
	{
		var sLinkUrl = oLink.getAttribute( '_fcksavedurl' ) ;
		if ( sLinkUrl == null )
			sLinkUrl = GetAttribute( oLink, 'href', '' ) ;
		if(sLinkUrl!='')
		    GetE('hyperlink').value	= sLinkUrl ;
	}
	return;
}
/************************************************/
function Ok(id,url)
{
    if(id==-1)
    {
        alert('File '+url+' KB,chon lai file kich thuoc nho hon.');
        return;
    }
    if(!oImageOriginal)
		oImageOriginal = oEditor.FCK.EditorDocument.createElement( "IMG" ) ;
    if(typeof(id)=='undefined' || typeof(url)=='undefined')
    {
        id = 0;
        url = oImageOriginal.src;
    }
    SetAttribute( oImageOriginal, 'width', GetE('image_width').value );
    SetAttribute( oImageOriginal, 'height', GetE('image_height').value );
    SetAttribute( oImageOriginal, 'hspace', GetE('hspace').value );
    SetAttribute( oImageOriginal, 'vspace', GetE('vspace').value );
    SetAttribute( oImageOriginal, 'border', GetE('oborder').value );
    /*Align*/
    var oalign = GetE('oalign')
    var ialign = oalign.selectedIndex;
    if(ialign!=0)
        SetAttribute( oImageOriginal, 'align', oalign[ialign].value );
    else
        SetAttribute( oImageOriginal, 'align');//Remove
    /*Style*/
    var ostyle = GetE('ostyle');
    var istyle = ostyle.selectedIndex;
    if ( oEditor.FCKBrowserInfo.IsIE )
	{
	    if(istyle!=0)
		    oImageOriginal.className = ostyle[istyle].value;
		else
		    oImageOriginal.className = '';
	}
	else
	{
	    if(istyle!=0)
		    SetAttribute( oImageOriginal, 'class', ostyle[istyle].value) ;
		else
		    SetAttribute( oImageOriginal, 'class', '') ;
		
	}
    var alt_ = GetE('description').value;
    alt_ = alt_.replace(/\x22|\x27/ig,'`');
    SetAttribute( oImageOriginal, 'alt', alt_);
    SetAttribute( oImageOriginal, 'src', url);
    
    var bHasImage = ( oImageOriginal != null ) ;
    oEditor.FCKUndo.SaveUndoStep() ;
    
    var sLnkUrl = GetE('hyperlink').value;
	if ( sLnkUrl.length == 0 )
	{
		if ( oLink )
			oEditor.FCK.ExecuteNamedCommand( 'Unlink' ) ;
	}
	else
	{
		if ( oLink )	// Modifying an existent link.
			oLink.href = sLnkUrl ;
		else			// Creating a new link.
		{
			if ( !bHasImage )
				oEditor.FCKSelection.SelectNode( oImageOriginal ) ;

			oLink = oEditor.FCK.CreateLink( sLnkUrl )[0] ;

			if ( !bHasImage )
			{
				oEditor.FCKSelection.SelectNode( oLink ) ;
				oEditor.FCKSelection.Collapse( false ) ;
			}
		}
		SetAttribute( oLink, '_fcksavedurl', sLnkUrl ) ;
        return true ;
	}
    oImageOriginal = oEditor.FCK.InsertElement(oImageOriginal) ;
    return true ;
};
function CheckUpload(){
    if (oImageOriginal){
        dialog.Selection.EnsureSelection() ;
        Ok(0,oImageOriginal.src);
        dialog.CloseDialog();
        return false;
    }
    return true;
};
// Fired when the width or height input texts change
var bLockRatio = true;
function SwitchLock( lockButton ){
    bLockRatio = !bLockRatio ;
    lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
    lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
    if ( bLockRatio ){
        if ( GetE('image_width').value.length > 0 )
            OnSizeChanged( 'Width', GetE('image_width').value ) ;
        else
            OnSizeChanged( 'Height', GetE('image_height').value ) ;
    }
};
function OnSizeChanged( dimension, value ){
    if ( oImageOriginal && bLockRatio ){
        var e = dimension == 'Width' ? GetE('image_height') : GetE('image_width') ;
        if ( value.length == 0 || isNaN( value ) ){
            e.value = '' ;
            return ;
        }
        if ( dimension == 'Width' )
            value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value / oImageOriginal.width ) ) ;
        else
            value = value == 0 ? 0 : Math.round( oImageOriginal.width * ( value / oImageOriginal.height ) ) ;
        if ( !isNaN( value ) )
            e.value = value ;
    }
};
function ResetSizes(){
    if ( ! oImageOriginal ) return ;
    if ( oEditor.FCKBrowserInfo.IsGecko && !oImageOriginal.complete ){
        setTimeout( ResetSizes, 50 ) ;
        return ;
    }
    GetE('image_width').value = oImageOriginal.width ;
    GetE('image_height').value = oImageOriginal.height ;
};